home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Dema / betonsoldier_spdemo.exe / {app} / Shaders / mesh_Fade_Shader.fx < prev    next >
Encoding:
Text File  |  2005-05-05  |  11.5 KB  |  356 lines

  1. //------------------------------------------------------------------------------------------------------
  2. //------------------------------------------------------------------------------------------------------
  3. //------------------------------------------------------------------------------------------------------
  4.  
  5. #include "shared.fx"
  6. #include "lighting.fx"
  7.  
  8. //------------------------------------------------------------------------------------------------------
  9. //- Static parameters
  10. //------------------------------------------------------------------------------------------------------
  11.  
  12. texture            DiffuseMap;
  13. float4            FadeFactor; //shared between all occurences of this shader.
  14.  
  15. //------------------------------------------------------------------------------------------------------
  16. //------------------------------------------------------------------------------------------------------
  17. //------------------------------------------------------------------------------------------------------
  18.  
  19. struct    FadeVertexShaderInput
  20. {
  21.     float4    Position    :    POSITION;
  22.     float2    DiffuseUv    :    TEXCOORD0;    
  23. };
  24.  
  25. //------------------------------------------------------------------------------------------------------
  26.  
  27. struct    FadeVertexShaderOutput
  28. {
  29.     float4    Position    :    POSITION;
  30.     float4    FadeFactor:    COLOR0;
  31.     float2    DiffuseUv    :    TEXCOORD0;    
  32. };
  33.  
  34. //------------------------------------------------------------------------------------------------------
  35.  
  36. struct    FadeSunlightVertexShaderOutput
  37. {
  38.     float4    Position                    :    POSITION;
  39.     float4    FadeFactor                :    COLOR0;
  40.     float2    DiffuseUv                    :    TEXCOORD0;    
  41.     float3     LightingComponent    : TEXCOORD1;
  42. };
  43.  
  44. //------------------------------------------------------------------------------------------------------
  45. //------------------------------------------------------------------------------------------------------
  46. //------------------------------------------------------------------------------------------------------
  47.  
  48. FadeVertexShaderOutput                    FadeVertexShaderTech1Pass1                (const FadeVertexShaderInput input);
  49. FadeSunlightVertexShaderOutput    FadeSunlightVertexShaderTech1Pass1(const FadeVertexShaderInput input);
  50.  
  51. //------------------------------------------------------------------------------------------------------
  52. //------------------------------------------------------------------------------------------------------
  53. //------------------------------------------------------------------------------------------------------
  54.  
  55. FadeVertexShaderOutput    FadeVertexShaderTech1Pass1(const FadeVertexShaderInput input)
  56. {
  57.     FadeVertexShaderOutput output;
  58.     
  59.     // output position into world+view+projection space
  60.     output.Position = mul (input.Position,WorldCameraProjection);
  61.         
  62.     // Diffuse Uv output
  63.     output.DiffuseUv = input.DiffuseUv;
  64.     
  65.     // use color0 to store the fade factor!!
  66.     output.FadeFactor = FadeFactor;
  67.         
  68.     return output;
  69. }
  70.  
  71. //------------------------------------------------------------------------------------------------------
  72.  
  73. #define SUN_IMPACT_RADIUS  700.0f
  74. #define SUN_FALLOFF_ZONE   1600.0f
  75. #define SUN_FALLOFF_SLOPE 1.0f / (SUN_IMPACT_RADIUS - SUN_FALLOFF_ZONE)
  76. #define SUN_ORIGIN_IMPACT_INTENSITY  1.0f - (SUN_IMPACT_RADIUS * SUN_FALLOFF_SLOPE)
  77.  
  78.  
  79. FadeSunlightVertexShaderOutput    FadeSunlightVertexShaderTech1Pass1(const FadeVertexShaderInput input)
  80. {
  81.     FadeSunlightVertexShaderOutput output;
  82.     
  83.     // output position into world+view+projection space
  84.     output.Position = mul (input.Position,WorldCameraProjection);
  85.         
  86.     // Diffuse Uv output
  87.     output.DiffuseUv = input.DiffuseUv;
  88.     
  89.     // use color0 to store the fade factor!!
  90.     output.FadeFactor = FadeFactor;
  91.  
  92.     // compute sunlight omni impact
  93.     float  omniDistanceFromVertex = distance(ObjectLocalLightPosition[0].xyz ,input.Position.xyz);    
  94.     output.LightingComponent.xyz = saturate( omniDistanceFromVertex * SUN_FALLOFF_SLOPE + SUN_ORIGIN_IMPACT_INTENSITY);
  95.     
  96.     return output;
  97. }
  98.  
  99. //------------------------------------------------------------------------------------------------------
  100. //------------------------------------------------------------------------------------------------------
  101. //------------------------------------------------------------------------------------------------------
  102.  
  103. technique    NoFade
  104. <
  105.     int Priority = 1;
  106.     int TechniqueIndex = 0;
  107.     int DeviceType = HWSHADER_ONLY;
  108.     int LightingType = INTEGRATED_LIGHTING;
  109.     string RenderingType = "Standard";
  110. >
  111. {
  112.     pass pass1
  113.     {
  114.         Texture[0]                = <DiffuseMap>;
  115.         MinFilter[0]            = LINEAR;
  116.         MagFilter[0]            = LINEAR;
  117.         MipFilter[0]            = LINEAR;
  118.         AddressU[0]                = WRAP;
  119.     AddressV[0]                = WRAP;
  120.         CullMode                    = CW;
  121.         ZEnable                        = true;
  122.         ZFunc                            = LESSEQUAL;
  123.         ZWriteEnable            = true;
  124.         AlphaBlendEnable    = false;
  125.         AlphaTestEnable        = true;
  126.         AlphaRef                    = 192;
  127.         AlphaFunc                    = GREATEREQUAL;
  128.         
  129.         VertexShader = compile vs_1_1 FadeVertexShaderTech1Pass1();
  130.         
  131.         PixelShader = 
  132.         asm
  133.         {
  134.             ps_1_1
  135.                          
  136.             tex        t0    // Diffuse map
  137.             
  138.             mov r0,t0 // ok take the texture alpha component as alpha, no pb.
  139.         };
  140.     }
  141. }
  142. //------------------------------------------------------------------------------------------------------
  143. //------------------------------------------------------------------------------------------------------
  144. //------------------------------------------------------------------------------------------------------
  145. technique    NoFadeWithAmbient
  146. <
  147.     int Priority = 1;
  148.     int TechniqueIndex = 1;
  149.     int DeviceType = HWSHADER_ONLY;
  150.     int LightingType = INTEGRATED_LIGHTING;
  151.     string RenderingType = "Standard";
  152. >
  153. {
  154.     pass pass1
  155.     {
  156.         Texture[0]                = <DiffuseMap>;
  157.         MinFilter[0]            = LINEAR;
  158.         MagFilter[0]            = LINEAR;
  159.         MipFilter[0]            = LINEAR;
  160.         AddressU[0]                = WRAP;
  161.     AddressV[0]                = WRAP;
  162.         CullMode                    = CW;
  163.         ZEnable                        = true;
  164.         ZFunc                            = LESSEQUAL;
  165.         ZWriteEnable            = true;
  166.         AlphaBlendEnable    = false;
  167.         AlphaTestEnable        = false;
  168. //    AlphaRef                    = 192;
  169. //    AlphaFunc                    = GREATEREQUAL;
  170.         
  171.         VertexShader = compile vs_1_1 FadeSunlightVertexShaderTech1Pass1();
  172.         
  173.         PixelShaderConstant[0]        = <AmbientColor>;
  174.         PixelShaderConstant[1]        = <DiffuseColor[0]>;
  175.         PixelShaderConstant[2]        = <DiffuseColor[1]>;
  176.         
  177.         PixelShader = 
  178.         asm
  179.         {
  180.             ps_1_1
  181.     
  182.             tex             t0            // Diffuse map
  183.             texcoord t1            // diffuse lighting component sunlight
  184.                 
  185. //        mad r0,c1,t1,t0                                                // add diffuse component sun  light to diffuse tex
  186. //        mad r0.rgb,c2,1-t1,r0                                    // add diffuse component back light
  187. //        mul    r0.rgb,r0,c0  + mov r0.a, c0.a        // ambient global modulate        
  188.     
  189.                         
  190. //        mul_sat            r0,t0,c0                    // ambient modulate                    
  191.             mad_sat r0,c2,1-t1,t0                    // add diffuse component back light
  192.             mul_sat r0.rgb, r0, c0                // ambient modulate only back light and texture
  193.             mad_sat r0.rgb,c1,t1,r0    + mov r0.a, c0.a         // add diffuse component sun light and fill alpha
  194.         };
  195.     }
  196. }
  197. //------------------------------------------------------------------------------------------------------
  198. //------------------------------------------------------------------------------------------------------
  199. //------------------------------------------------------------------------------------------------------
  200.  
  201. technique    Fade
  202. <
  203.     int Priority = 0;
  204.     int NeedSorting    = 1;
  205.     int TechniqueIndex = 2;
  206.     int DeviceType = HWSHADER_ONLY;
  207.     int LightingType = INTEGRATED_LIGHTING;
  208.     string RenderingType = "Standard";
  209. >
  210. {
  211.     pass pass1
  212.     {
  213.         Texture[0]            = <DiffuseMap>;
  214.         MinFilter[0]        = LINEAR;
  215.         MagFilter[0]        = LINEAR;
  216.         MipFilter[0]        = LINEAR;
  217.         AddressU[0]            = WRAP;
  218.     AddressV[0]            = WRAP;
  219.         CullMode                = CW;
  220.         ZEnable                    = true; 
  221.         ZFunc                        = LESSEQUAL;
  222.         ZWriteEnable        = false;
  223.         
  224.         AlphaTestEnable        = false; // eliminate too thin pixels
  225. //    AlphaRef                    = 100;
  226. //    AlphaFunc                    = GREATER;
  227.         AlphaBlendEnable    = true;        //use alpha blend !!! I beg thee.
  228.         SrcBlend                    = SRCALPHA;
  229.         DestBlend                    = INVSRCALPHA;        
  230.         
  231.         VertexShader = compile vs_1_1 FadeVertexShaderTech1Pass1();
  232.         
  233.         PixelShader = 
  234.         asm
  235.         {
  236.             ps_1_1
  237.                          
  238.             tex        t0    // Diffuse map
  239.                         
  240.             mov r0.rgb,t0 + mul r0.a, t0.a, v0.a // ponderate the texture alpha by a global alpha blend -> fade. 
  241.         };
  242.     }
  243. }
  244.  
  245. //------------------------------------------------------------------------------------------------------
  246. //------------------------------------------------------------------------------------------------------
  247. //------------------------------------------------------------------------------------------------------
  248. technique    FadeWithAmbient
  249. <
  250.     int Priority = 0;
  251.     int NeedSorting    = 1;
  252.     int TechniqueIndex = 3;
  253.     int DeviceType = HWSHADER_ONLY;
  254.     int LightingType = INTEGRATED_LIGHTING;
  255.     string RenderingType = "Standard";
  256. >
  257. {
  258.     pass pass1
  259.     {
  260.         Texture[0]            = <DiffuseMap>;
  261.         MinFilter[0]        = LINEAR;
  262.         MagFilter[0]        = LINEAR;
  263.         MipFilter[0]        = LINEAR;
  264.         AddressU[0]            = WRAP;
  265.     AddressV[0]            = WRAP;
  266.         CullMode                = CW;
  267.         ZEnable                    = true; 
  268.         ZFunc                        = LESSEQUAL;
  269.         ZWriteEnable        = false;
  270.         
  271.         AlphaTestEnable        = false; // eliminate too thin pixels
  272. //    AlphaRef                    = 100;
  273. //    AlphaFunc                    = GREATER;
  274.         AlphaBlendEnable    = true;        //use alpha blend !!! I beg thee.
  275.         SrcBlend                    = SRCALPHA;
  276.         DestBlend                    = INVSRCALPHA;        
  277.         
  278.         VertexShader = compile vs_1_1 FadeSunlightVertexShaderTech1Pass1();
  279.  
  280.         PixelShaderConstant[0]        = <AmbientColor>;
  281.         PixelShaderConstant[1]        = <DiffuseColor[0]>;
  282.         
  283.         PixelShader = 
  284.         asm
  285.         {
  286.             ps_1_1
  287.                          
  288.             tex        t0            // Diffuse map
  289.             texcoord t1        // diffuse lighting component
  290.             
  291.         //mul r0,t0,t0
  292.             mul r1,t0,c0                    // ambient modulate
  293.             mul r0,t0,t1                    // modulate diffuse by sun aura    
  294.             mad r0.rgb,c1,r0,r1    + mul r0.a, t0.a, v0.a // add diffuse component and ponderate alpha by global alpha                    
  295.         };
  296.     }
  297. }
  298.  
  299. //------------------------------------------------------------------------------------------------------
  300. //------------------------------------------------------------------------------------------------------
  301. //------------------------------------------------------------------------------------------------------
  302. technique    techTnL_0
  303. <
  304.     int Priority                 = 1;
  305.     int TechniqueIndex     = 0;
  306.     int DeviceType             = TNL_ONLY;
  307.     int LightingType         = INTEGRATED_LIGHTING;
  308.     string RenderingType = "Standard";
  309. >
  310. {
  311.     pass pass1
  312.     {
  313.         WorldTransform[0]    = <WorldCameraProjection>;
  314.         
  315.         Texture[0]            = <DiffuseMap>;
  316.         
  317.         ColorArg1[0]        = TEXTURE;
  318.         ColorOp[0]            = SELECTARG1;
  319.         AlphaArg1[0]        = TEXTURE;
  320.         AlphaOp[0]            = SELECTARG1;
  321.         
  322.         ColorOp[1]            = DISABLE;
  323.         AlphaOp[1]            = DISABLE;
  324.         
  325.         MinFilter[0]        = LINEAR;
  326.         MagFilter[0]        = LINEAR;
  327.         MipFilter[0]        = LINEAR;
  328.         
  329.         AddressU[0]                = WRAP;
  330.     AddressV[0]                = WRAP;
  331.         
  332.         CullMode                    = CW;
  333.         ZEnable                        = true;
  334.         ZFunc                            = LESSEQUAL;
  335.         ZWriteEnable            = true;
  336.         AlphaBlendEnable    = false;
  337.         AlphaTestEnable        = true;
  338.         AlphaRef                    = 192;
  339.         AlphaFunc                    = GREATEREQUAL;
  340.         
  341.         VertexShader             = NULL;        
  342.         PixelShader             = NULL;
  343.     }
  344. }
  345. //------------------------------------------------------------------------------------------------------
  346. //------------------------------------------------------------------------------------------------------
  347. //------------------------------------------------------------------------------------------------------
  348.  
  349. #include "mesh_shadow.fx"
  350. #include "mesh_shadow_projector.fx"
  351.  
  352. //------------------------------------------------------------------------------------------------------
  353. //------------------------------------------------------------------------------------------------------
  354. //------------------------------------------------------------------------------------------------------
  355.  
  356.